'use strict';

const { ACTION_TYPE } = require('../constants/actionType');

// 获取Controller类
const Controller = require('egg').Controller;

class RecyclerController extends Controller {
  async restore() {
    const { ctx } = this;
    const { userId } = ctx.helper.jsonToHump(ctx.decode);
    const { regenerantIds } = ctx.request.body;
    const success = await ctx.service.recycler.restore(userId, JSON.parse(regenerantIds));
    await ctx.service.record.create(userId, ACTION_TYPE.RestorePicture, `从回收站中恢复了${regenerantIds.length}张照片`);
    ctx.body = ctx.helper.formatResponse({ success }, 200, '恢复照片成功');
  }

  async destory() {
    const { ctx } = this;
    const { userId } = ctx.helper.jsonToHump(ctx.decode);
    const { regenerantIds } = ctx.request.query;
    const success = await ctx.service.recycler.destory(JSON.parse(regenerantIds));
    await ctx.service.record.create(userId, ACTION_TYPE.DeletePicture, `从回收站中永久删除了${regenerantIds.length}张照片`);
    ctx.body = ctx.helper.formatResponse({ success }, 200, '永久删除照片成功');
  }

  async clear() {
    const { ctx } = this;
    const { userId } = ctx.helper.jsonToHump(ctx.decode);
    const success = await ctx.service.recycler.clear(userId);
    await ctx.service.record.create(userId, ACTION_TYPE.ClearRecycler, '清空回收站');
    ctx.body = ctx.helper.formatResponse({ success }, 200, '清空回收站成功');
  }

  async index() {
    const { ctx } = this;
    const { userId } = ctx.helper.jsonToHump(ctx.decode);
    const { page, size } = ctx.request.query;
    const { list, total } = await ctx.service.recycler.index(userId, page, size);
    ctx.body = ctx.helper.formatResponse({ list: ctx.helper.jsonToHump(list), total }, 200, '获取回收站列表成功');
  }
}

module.exports = RecyclerController;
